// LGUIChannelSelector.cpp: implementation of the LGUIChannelSelector class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LGUIWindow.h"
#include "LGUIContainer.h"
#include "LGUIMessage.h"
#include "LGUIChannelSelector.h"
#include "LAMEBuffer.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
LGUIChannelSelector::LGUIChannelSelector(int id, int _x, int _y) : LGUIWindow(_x,_y,BSIZE*16,BSIZE)
{
  ID=id;
  channel=0;
}

LGUIChannelSelector::~LGUIChannelSelector()
{
  
}



void LGUIChannelSelector::draw(int xoff, int yoff)
{
  if (isDirty)
  {
    static TCHAR text[]=TEXT("1 2 3 4 5 6 7 8 9 10111213141516");
    for (int i=0; i<_tcslen(text); i++)
    {
      if (text[i]==' ') text[i]=0;
    }
    TCHAR temp[3];
    temp[2]=0;
    for (int i=0; i<16; i++)
    {
      int color=i==channel ? COLOR(0xff4040) : COLOR(0x800000);
      if (i==9)
        color = i==channel ? COLOR(0x4040FF) : COLOR(0x000080);
      buffer.rectFillFaded(x+xoff+i*BSIZE,y+yoff,x+xoff+(i+1)*BSIZE,y+yoff+h, (color),darkenColor(color));
      buffer.rect(x+xoff+i*BSIZE,y+yoff,x+xoff+(i+1)*BSIZE,y+yoff+h,0);
      temp[0]=text[i*2];
      temp[1]=text[i*2+1];
      buffer.drawTextShadowCenter(temp,x+xoff+i*BSIZE+BSIZE/2-1,y+yoff+BSIZE/2-1);
    }
    buffer.rect(x+xoff+channel*BSIZE,y+yoff,x+xoff+(channel+1)*BSIZE,y+yoff+h,channel!=9?COLOR(0xff8f8f):COLOR(0x8f8fff));
    
    LRect rect = *this;
    rect.w++;
    rect.h++;
    buffer.addDirtyRect(rect);
    
    isDirty=false;
  }
}


void LGUIChannelSelector::penDown(int px, int py)
{
  if (isInside(px,py))
  {
    px-=x;
    int num = px/BSIZE;
    if (num<16)
    {
      channel=num;
      if (parent)
        parent->deliverMessage(LGUIMessage(MSG_BUTTONPRESSED, ID, channel));
    }
    
    redraw();
  }
}

